package com.lezhi.socialgram.service.sys.impl;

import com.lezhi.socialgram.enums.ResultEnum;
import com.lezhi.socialgram.exception.SocialGramException;
import com.lezhi.socialgram.mapper.sys.UserMapper;
import com.lezhi.socialgram.model.sys.UserInfo;
import com.lezhi.socialgram.service.sys.UserService;
import com.lezhi.socialgram.util.Param;
import com.lezhi.socialgram.util.Result;
import com.lezhi.socialgram.util.ResultUtils;
import com.lezhi.socialgram.util.TableUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

/**
 * Created by daizhen on 2017/4/19.
 */
@Service
public class UserServiceImpl implements UserService {

    private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private UserMapper userMapper;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Override
    public Result updateUserInfo() {
        return null;
    }

    @Override
    public Result getAccountInfo(Long userId) {
        try {
            UserInfo userInfo = new UserInfo();
            userInfo.setTable(TableUtils.getTableNameWithId(UserInfo.class, userId, 10));
            userInfo.setUserId(userId);
            UserInfo info = userMapper.queryUserInfo(userInfo);
            Long likeCount = stringRedisTemplate.opsForHash().get(userId + "user_count", Param.LIKE_COUNT) != null ?
                    Long.parseLong(stringRedisTemplate.opsForHash().get(userId + "user_count", Param.LIKE_COUNT) + "") : 0;
            Long likeMeCount = stringRedisTemplate.opsForHash().get(userId + "user_count", Param.LIKE_ME_COUNT) != null ?
                    Long.parseLong(stringRedisTemplate.opsForHash().get(userId + "user_count", Param.LIKE_ME_COUNT) + "") : 0;
            Long likeMyMomentCount = stringRedisTemplate.opsForHash().get(userId + "user_count", Param.LIKE_MY_MOMENT_TOTAL_COUNT) != null ?
                    Long.parseLong(stringRedisTemplate.opsForHash().get(userId + "user_count", Param.LIKE_MY_MOMENT_TOTAL_COUNT) + "") : 0;
            Long grantToMeCount = stringRedisTemplate.opsForHash().get(userId + "user_count", Param.GRANT_TO_ME_COUNT) != null ?
                    Long.parseLong(stringRedisTemplate.opsForHash().get(userId + "user_count", Param.GRANT_TO_ME_COUNT) + "") : 0;
            Long seendMyMomentCount = stringRedisTemplate.opsForHash().get(userId + "user_count", Param.SEEN_MY_MOMENT_TOTAL_COUNT) != null ?
                    Long.parseLong(stringRedisTemplate.opsForHash().get(userId + "user_count", Param.SEEN_MY_MOMENT_TOTAL_COUNT) + "") : 0;
            info.setMyLikeCount(likeCount);
            info.setLikeMeCount(likeMeCount);
            info.setGiftsTotalCount(grantToMeCount);
            info.setMomentSeenTotalCount(seendMyMomentCount);
            info.setMomentLikedTotalCount(likeMyMomentCount);
            return ResultUtils.success(info);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new SocialGramException(ResultEnum.INTERNAL_ERROR);
        }
    }
}
